package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.base.BaseResult;
import com.ls.base.BaseResultUtil;
import com.ls.controller.model.serviceIp.AddServiceIpModel;
import com.ls.controller.model.serviceIp.EditServiceIpModel;
import com.ls.controller.model.serviceIp.QueryServiceIpModel;
import com.ls.entity.CommChannel;
import com.ls.entity.CommServiceIp;
import com.ls.entity.SysUser;
import com.ls.exception.SysException;
import com.ls.mapper.CommChannelMapper;
import com.ls.mapper.CommServiceIpMapper;
import com.ls.service.ICommServiceIpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.ls.base.BaseResultUtil.SQL_ERROR;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2019-12-06
 */
@Service
public class CommServiceIpServiceImpl extends ServiceImpl<CommServiceIpMapper, CommServiceIp> implements ICommServiceIpService {

    @Autowired
    CommServiceIpMapper mapper;

    @Override
    public BaseResult add(AddServiceIpModel model, SysUser currentUser) {
        boolean checkFlag = checkServiceIpExist(model.getServiceId(),model.getIp());
        if (checkFlag) {
            return BaseResultUtil.error("关联关系已存在");
        }
        CommServiceIp serviceIp = new CommServiceIp();
        BeanUtils.copyProperties(model, serviceIp);
        serviceIp.setCreateTime(LocalDateTime.now());
        serviceIp.setCreateUserId(currentUser.getUserId());
        serviceIp.setUpdateTime(LocalDateTime.now());
        serviceIp.setUpdateUserId(currentUser.getUserId());
        int result = mapper.insert(serviceIp);
        if (result > 0) {
            return BaseResultUtil.success();
        } else {
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    private boolean checkServiceIpExist(Long serviceId, String ip) {
        CommServiceIp serviceIp = mapper.selectOne(new QueryWrapper<CommServiceIp>().eq("service_id",serviceId).eq("ip",ip));
        if(serviceIp ==null){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public BaseResult edit(EditServiceIpModel model, SysUser currentUser) {
        Long serviceIpId = model.getServiceIpId();
        CommServiceIp serviceIp = mapper.selectById(serviceIpId);
        if (serviceIp == null) {
            throw new SysException("serviceIpId: "+ serviceIpId +" 在库里不存在");
        }
        String ipNew = model.getIp();
        if (!serviceIp.getIp().equals(ipNew)) {
            Long serviceId = serviceIp.getServiceId();
            boolean commChannelCheck = checkIpExist(serviceId,ipNew);
            if (commChannelCheck) {
                throw new SysException("ip:"+ipNew + " 与当前业务 "+ serviceId +"的绑定，在库里已经存在");
            }
        }else{
            throw new SysException("ip与库里一致，无需修改");
        }
        BeanUtils.copyProperties(model, serviceIp);
        serviceIp.setUpdateTime(LocalDateTime.now());
        serviceIp.setUpdateUserId(currentUser.getUserId());
        int result = mapper.updateById(serviceIp);
        if (result > 0) {
            return BaseResultUtil.success();
        } else {
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    private boolean checkIpExist(Long serviceId, String ip) {
        CommServiceIp serviceIp = mapper.selectOne(new QueryWrapper<CommServiceIp>().eq("service_id",serviceId).eq("ip",ip));
        if(serviceIp == null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public BaseResult listByPage(QueryServiceIpModel model, SysUser currentUser) {
        QueryWrapper<CommServiceIp> wrapper = new QueryWrapper<CommServiceIp>().eq("service_id",model.getServiceId());
        IPage<CommServiceIp> pageData = mapper.selectPage(new Page<>(model.getPageNum(), model.getPageSize()), wrapper);
        return BaseResultUtil.success(pageData);
    }
}
